package com.web.handler.controller;

import java.util.List;
import java.util.Map;

import com.web.common.enums.BaseStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.alibaba.fastjson.JSONObject;
import com.web.accountingEntry.service.AccountingEntryService;
import com.web.accumulate.service.AccumulateService;
import com.web.common.BaseResult;
import com.web.common.ReturnCode;
import com.web.handler.entity.ProviderTransaction;
import com.web.handler.entity.QuotePrice;
import com.web.handler.service.ProviderTransactionService;
import com.web.handler.service.QuotePriceService;
import com.web.position.entity.Position;
import com.web.position.service.PositionService;
import com.web.transaction.entity.Transaction;
import com.web.transaction.service.TransactionService;
import com.web.transactionMonitor.service.TransactionMonitorService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(value = "API - Provider",description = "provider")
@RestController
@RequestMapping("/provider")
@CrossOrigin
public class ProviderController {

	@Autowired
	private ProviderTransactionService providerTransactionService;

	@Autowired
    private TransactionService transactionService;

	@Autowired
    private PositionService positionService;

	@Autowired
    private AccumulateService accumulateService;

	@Autowired
    private QuotePriceService quotePriceService;

	@Autowired
    private AccountingEntryService accountingEntryService;

	@Autowired
    private TransactionMonitorService transactionMonitorService;

	@RequestMapping(value = "/fxDealHandler",method = RequestMethod.POST)
    @ApiOperation(value = "fxDeal接口")
    public BaseResult fxDeal(@ModelAttribute ProviderTransaction fxd){

		//核心交易入库
		Transaction transaction = providerTransactionService.convertToTransaction(fxd);
		transactionService.addTransaction(transaction);

		//拆盘
		List<Position> list = positionService.genPositions(transaction);
		if(list.size() == 0) {
			return new BaseResult(BaseStatus.POSITION_DATA_NOT_EXIST.getCode(),BaseStatus.POSITION_DATA_NOT_EXIST.getDesc());
		}

		//监控累总
		transactionMonitorService.accumulate(list);

		//callBoc
		providerTransactionService.callCore(list);

		//判断累总条件并累总
		accumulateService.accumulate(list);

		//上盘
		providerTransactionService.uploadToAccumulated(transaction);

        //生成账务数据
        accountingEntryService.genAccountingEntries(list);

		return new BaseResult(BaseStatus.SUCCESS.getCode(),BaseStatus.QUOTE_PRICE_SUCCESS.getDesc());
    }

	@RequestMapping(value = "/quotePriceHandler",method = RequestMethod.POST)
    @ApiOperation(value = "quotePrice接口")
    public BaseResult quotePrice(@ModelAttribute QuotePrice quotePrice){
		quotePriceService.checkInput(quotePrice);

		System.out.println(quotePrice.toString());

		quotePriceService.calculateAmt(quotePrice);

		quotePriceService.addQuotePrice(quotePrice.markNew());

		return new BaseResult(BaseStatus.SUCCESS.getCode(),quotePrice) ;
	}

	@RequestMapping(value = "/quotePriceCancel",method = RequestMethod.POST)
    @ApiOperation(value = "取消quotePrice")
    public BaseResult CancelQuotePrice(@RequestParam("quotePriceNo") String quotePriceNo){
		QuotePrice quotePrice = new QuotePrice();
		quotePrice.setQuotePriceNo(quotePriceNo);
		quotePrice.setStatus(QuotePrice.QUOTEPRICE_STATUS_CANCEL);

		quotePriceService.amendQuotePrice(quotePrice);

		return new BaseResult<>(BaseStatus.SUCCESS.getCode(),BaseStatus.FCE_CANCEL_SUCCESS.getDesc());
	}


	@RequestMapping(value = "/getEcharts")
	public BaseResult getEchatsList(){

		List<Map<String,String>> quoteMap= quotePriceService.getEcharts();

		return new BaseResult<>(BaseStatus.SUCCESS.getCode(),quoteMap);
	}

}
